home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Learn C++ (CodeWarrior) / Chap 09.06 - memberWise / memberWise.cp < prev    next >
Text File  |  1995-10-21  |  2KB  |  88 lines

  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4.  
  5. //---------------------------------------  Name
  6.  
  7. class Name
  8. {
  9.     private:
  10.         char    *last;
  11.         char    *first;
  12.  
  13.     public:
  14.                 Name( char *firstParam, char *lastParam );
  15.                 Name( const Name &original );
  16.                 ~Name();
  17.         void    Display();
  18.         Name    &operator=( const Name &original );
  19. };
  20.  
  21. Name::Name( char *firstParam, char *lastParam )
  22. {
  23.     first = new char[ strlen(firstParam) + 1 ];
  24.     last = new char[ strlen(lastParam) + 1 ];
  25.     
  26.     strcpy( first, firstParam );
  27.     strcpy( last, lastParam );
  28.  
  29.     cout << "Original constructor...\n";
  30. }
  31.  
  32. Name::Name( const Name &original )
  33. {
  34.     first = new char[ strlen(original.first) + 1 ];
  35.     last = new char[ strlen(original.last) + 1 ];
  36.     
  37.     strcpy( first, original.first );
  38.     strcpy( last, original.last );
  39.     
  40.     cout << "Copy constructor...\n";
  41. }
  42.  
  43. Name::~Name()
  44. {
  45.     delete [] first;
  46.     delete [] last;
  47. }
  48.  
  49. void    Name::Display()
  50. {
  51.     cout << "Name: " << first << " " << last << "\n";
  52. }
  53.  
  54. Name    &Name::operator=( const Name &original )
  55. {
  56.     if ( this == &original )
  57.         return( *this );
  58.         
  59.     delete [] first;
  60.     delete [] last;
  61.     
  62.     first = new char[ strlen(original.first) + 1 ];
  63.     last = new char[ strlen(original.last) + 1 ];
  64.     
  65.     strcpy( first, original.first );
  66.     strcpy( last, original.last );
  67.     
  68.     return( *this );
  69. }
  70.  
  71.  
  72. //---------------------------------------  main()
  73.  
  74. int    main()
  75. {
  76.     Name    yourAuthor( "Dave", "Mark" );
  77.     Name    aCopy = yourAuthor;
  78.     Name    anotherAuthor( "Scott", "Knaster" );
  79.     
  80.     yourAuthor.Display();
  81.     aCopy.Display();
  82.     
  83.     aCopy = anotherAuthor;
  84.     
  85.     aCopy.Display();
  86.     
  87.     return 0;
  88. }